package com.qiyeji.real_estate.ProjectTask;

import com.qiyeji.real_estate.entity.TemporaryWorkKey;
import com.qiyeji.real_estate.entity.workKey;
import com.qiyeji.real_estate.service.WorkService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.TaskListener;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Repository
public class TaskListenerForLateWorker implements TaskListener {

    @Autowired
    WorkService workService;

    @Autowired
    RuntimeService runtimeService;

    /**用来指定任务的办理人*/
    @Override
    public void notify(DelegateTask delegateTask) {
        //指定个人任务的办理人，也可以指定组任务的办理人
        //个人任务：通过类去查询数据库，将下一个任务的办理人查询获取，然后通过setAssignee()的方法指定任务的办理人
        System.out.println(delegateTask.getName());
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(delegateTask.getProcessInstanceId()).singleResult();
        List<TemporaryWorkKey> workKeys = workService.fuzzyQuery1(processInstance.getBusinessKey());
        List<String> lateWorkers = new ArrayList<>();
        for (TemporaryWorkKey workKey: workKeys)
            lateWorkers.add(workKey.getScode());
        delegateTask.setVariable("lateWorkers", lateWorkers);
    }
}
